'use strict';

const mongoose = require('mongoose'),
    foodModel = mongoose.model('Food');

module.exports = {
    addFood: addFood,
    deleteFood: deleteFood,
    updateFood: updateFood,  
    getFoodByFname: getFoodByFname, 
    getFoodByIcnum: getFoodByIcnum,
    getFoodByFtype: getFoodByFtype,
    getFoodByIcnumFtype: getFoodByIcnumFtype,
    getFoodById: getFoodById,
}

function addFood(food,callback){
    foodModel.create(food,function(error,data){
        callback(null,data);
    });
}

function deleteFood(_id,callback){
    foodModel.remove({'_id':_id},function(error,data){
        callback(null,data);
    });
}

function updateFood(food,callback){
    foodModel.update({'_id':food._id},{
        fname: food.fname,
        fprice: food.fprice,
        ftype: food.ftype,
        fimg: food.fimg,
        merchantIcnum: food.merchantIcnum,
    },function(error,data){
        callback(null,data);
    });
}

function getFoodByFname(fname,callback){
    foodModel.find({'fname':fname},function(error,data){
        callback(null,data);
    });
}

function getFoodByFtype(ftype,callback){
    foodModel.find({'ftype':ftype},function(error,data){
        callback(null,data);
    });
}

function getFoodByIcnum(icnum,callback){
    foodModel.find({merchantIcnum : icnum},function(error,data){
        callback(null,data);
    });
}

function getFoodByIcnumFtype(icnum,ftype,callback){
    foodModel.find({
        merchantIcnum : icnum,
        ftype : ftype
        },function(error,data){
        callback(null,data);
    });
}

function getFoodById(_id,callback){
    foodModel.find({'_id':_id},function(error,data){
        callback(null,data);
    })
}